/*
 * Copyright 2010 Gaurav Saxena
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwtstructs.gwt.client.widgets.stylesheets;

public class CssImplIE extends CssImpl{
	@Override
	public native boolean editRule(String ruleName, String styleAttribute, String attributeValue) /*-{
		for(var i = 0, len = $doc.styleSheets.length; i < len; i++) {
			var cssrules = $doc.styleSheets[i].rules;
			if(editFromAllCssRule(cssrules, ruleName, styleAttribute, attributeValue))
				return true;
		}
		return false;
				
		function editFromAllCssRule(cssrules, ruleName, styleAttribute, attributeValue){
			for(var i = 0; i < cssrules.length; i++) {
				if(cssrules[i].selectorText.indexOf('@import') > -1 && editFromAllCssRule(cssrules[i].styleSheet.rules, ruleName, styleAttribute, attributeValue))
					return true;
				else if(cssrules[i].selectorText.toLowerCase().indexOf(ruleName.toLowerCase()) > -1) {
					cssrules[i].style[styleAttribute] = attributeValue;
					return true;
				}
			}
			return false;
		}
	}-*/;
	@Override
	public native String getRule(String ruleName)/*-{
		for(var i = 0, len = $doc.styleSheets.length; i < len; i++) {
			var cssrules = $doc.styleSheets[i].rules;
			var cssRule = getFromAllCssRules(cssrules, ruleName);
			if(cssRule != "")
				return cssRule;
		}
		return "";
		function getFromAllCssRules(cssrules, ruleName) {
			for(var i = 0; i < cssrules.length; i++) {
				if(cssrules[i].selectorText.indexOf('@import') > -1) {
					var cssRule = getFromAllCssRules(cssrules[i].styleSheet.rules, ruleName);
					if(cssRule != "")
						return cssRule;
				}
				else if(cssrules[i].selectorText.toLowerCase() == ruleName.toLowerCase())
					return cssrules[i].cssText;
			}
			return "";
		}
	}-*/;
	@Override
	public native void addRule(String ruleName, String ruleBody)/*-{
		this.@com.gwtstructs.gwt.client.widgets.stylesheets.CssImplIE::deleteRule(Ljava/lang/String;)(ruleName);
		$wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].addRule(ruleName, ruleBody);
	}-*/;
	@Override
	public native boolean deleteRule(String ruleName)/*-{
		var cssrules = $wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].rules;
		for(var i = 0; i < cssrules.length; i++)
		{
			if(cssrules[i].selectorText.toLowerCase() == ruleName.toLowerCase())
			{
				$wnd.document.styleSheets[$wnd.document.styleSheets.length - 1].removeRule(i);
				return true;
			}
		}
		return false;
	}-*/;
}
